-- UIChannelSelect
-- Created by chengb Oct/14/2015
-- ios下的渠道选择窗口

UIChannelSelect = class("UIChannelSelect", function()
    return cc.Layer:create();
end);

function UIChannelSelect.create()
    return UIChannelSelect.new();
end

-- 按钮点击频率
local CLICK_CD_TIME = 0.3;

-- 构造函数
function UIChannelSelect:ctor()
    self:setName("UIChannelSelect");

    -- 初始化
    local csbName = iif(PUBLISH_PLATFORM == PUBLISH_TYPE_APP_STORE, "ChannelSelect.csb", "ChannelSelectCN.csb")
    local node = cc.CSLoader:createNode("layout/login/ios/" .. csbName);
    self:addChild(node);
    self.node = node;

    -- 注册点击事件
    self:registerTouchEvent();

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIChannelSelect:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 重绘
function UIChannelSelect:redraw()
    local kerning = 0;
    local fontSize = TextStyleM.TEXT_SIZE_MIDDLE;
    if not isFullWidthLang() then
        -- 英文版本需要缩小文本间距
        kerning = -5;
        fontSize = TextStyleM.TEXT_SIZE_SMALL;
    end

    local fontName = getLocFontName();

    -- 游客登录
    local btnGuest = findChildByName(self.node, "CT/btn_guest");
    local titleLabel = findChildByName(btnGuest, "title");
    titleLabel:setFontName(fontName);
    titleLabel:setFontSize(fontSize);
    titleLabel:setTextColor(TextStyleM.TEXT_COLOR_WHITE);
    local outlineColor = TextStyleM.TEXT_OUTLINE_COLOR_GUEST;
    titleLabel:enableOutline(outlineColor, 2);
    titleLabel:getVirtualRenderer():setAdditionalKerning(kerning);
    titleLabel:setString(getLocStr("btn_text_guest_login"));

    -- GameCenter登录
    local btnGamecenter = findChildByName(self.node, "CT/btn_gamecenter");
    local titleLabel = findChildByName(btnGamecenter, "title");
    titleLabel:setFontName(fontName);
    titleLabel:setFontSize(fontSize);
    titleLabel:setTextColor(TextStyleM.TEXT_COLOR_WHITE);
    local outlineColor = TextStyleM.TEXT_OUTLINE_COLOR_GAMECENTER;
    titleLabel:enableOutline(outlineColor, 2);
    titleLabel:getVirtualRenderer():setAdditionalKerning(kerning);
    titleLabel:setString(getLocStr("btn_text_gamecenter_login"));

    -- 海外AppStore
    if PUBLISH_PLATFORM == PUBLISH_TYPE_APP_STORE then
        -- Facebook登录
        local btnFacebook = findChildByName(self.node, "CT/btn_facebook");
        local titleLabel = findChildByName(btnFacebook, "title")
        titleLabel:setFontName(fontName);
        titleLabel:setFontSize(fontSize);
        titleLabel:setTextColor(TextStyleM.TEXT_COLOR_WHITE);
        local outlineColor = TextStyleM.TEXT_OUTLINE_COLOR_FACEBOOK;
        titleLabel:enableOutline(outlineColor, 2);
        titleLabel:getVirtualRenderer():setAdditionalKerning(kerning);
        titleLabel:setString(getLocStr("btn_text_facebook_login"));
    end
end

-- 进入通信显示模式
function UIChannelSelect:enterCommunicateMode()
    -- 关闭当前界面
    closeFormByName("UIChannelSelect");

    local uiLogin = UIMgr.getCurrentScene():getFormByName("UILogin");
    if uiLogin then
        uiLogin:enterCommunicateMode();
    end
end

-- 尝试进行游客登录
function UIChannelSelect:tryGuestLogin()
    local function okFunc()
        -- 尝试进行游客登录
        if not LoginM.tryGuestLogin() then
            return;
        end

        -- 进入通信显示
        self:enterCommunicateMode();
    end

    if getLastAccountChannel() ~= "" then
        -- 已经登录过游戏
        okFunc();
        return;
    end

    -- 从未登录过游戏，需要弹出提示
    confirm2(CONFIRM_TYPE_OK, getLocStr("guest_mode"), getLocStr("guest_mode_tip"), okFunc);
end

-- 注册按钮点击事件
function UIChannelSelect:registerTouchEvent()
    -- 注册游客登录按钮点击事件
    local btnGuest = findChildByName(self.node, "CT/btn_guest");
    local function onGuestClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 限制点击频率
            setClickFrequency(sender, CLICK_CD_TIME);

            -- 尝试进行游客登录
            self:tryGuestLogin();
        end
    end
    btnGuest:addTouchEventListener(onGuestClick);

    if PUBLISH_PLATFORM == PUBLISH_TYPE_APP_STORE then
        -- 注册Facebook登录按钮点击事件
        local btnFacebook = findChildByName(self.node, "CT/btn_facebook");
        local function onFacebookClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                -- 限制点击频率
                setClickFrequency(sender, CLICK_CD_TIME);

                local channel = getFacebookChannelName();
                if hasLogin3rdPlatform(channel) then
                    trace("UIChannelSelect", "玩家已经登录facebook账号，直接请求服务器认证。");
                    auth3rdPlatform(channel);

                    -- 进入通信显示
                    self:enterCommunicateMode();
                    return;
                end

                -- 打上请求标识
                local path = string.format("request_%s_login", channel);
                _G[path] = true;

                -- 打开facebook登录窗口
                showFacebookLogin();
            end
        end
        btnFacebook:addTouchEventListener(onFacebookClick);
    end

    -- 注册GameCenter登录按钮点击事件
    local btnGamecenter = findChildByName(self.node, "CT/btn_gamecenter");
    local function onGamecenterClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 限制点击频率
            setClickFrequency(sender, CLICK_CD_TIME);

            local channel = CHANNEL_GAME_CENTER;
            if hasLogin3rdPlatform(channel) then
                trace("UIChannelSelect", "玩家已经登录GameCenter账号，直接请求服务器认证。");
                auth3rdPlatform(channel);

                -- 进入通信显示
                self:enterCommunicateMode();
                return;
            end

            -- 打上请求标识
            _G["request_gamecenter_login"] = true;
            show3rdAccountLogin(CHANNEL_GAME_CENTER);
        end
    end
    btnGamecenter:addTouchEventListener(onGamecenterClick);

    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIChannelSelect");
        end
    end
    AddTouchEventListener(btnBack, onBackClick);
    AddBackEventHandler(self, btnBack, btnBack.touchEventListener);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
end

-- 注册事件处理回调函数
function UIChannelSelect:registerEventCallback()
    self:registerScriptHandler(function(ev)
        if ev == "enter" then
            -- 显示登录界面的遮罩
            local uiLogin = UIMgr.getCurrentScene():getFormByName("UILogin");
            uiLogin:setMaskVisible(true);

        elseif ev == "exit" then
            -- 反注册所有事件
            EventMgr.removeAll("UIChannelSelect");

            -- 隐藏登录界面的遮罩
            local uiLogin = UIMgr.getCurrentScene():getFormByName("UILogin");
            uiLogin:setMaskVisible(false);
        end
    end);
end








